/*
 * SearchControl.fx
 *
 * Created on 05-may-2009, 16:28:21
 */

package webooksfx.controller;

import java.lang.Exception;
import webooksfx.controller.Control;
import webooksfx.controller.DetailControl;
import webooksfx.controller.LoadingControl;
import webooksfx.model.ColeccionLibros;
import webooksfx.model.Servicio;
import webooksfx.view.SearchView;

/**
 * @author Administrador
 */

public class SearchControl extends Control {
	public var visibilidad : Boolean = false; //Variable a la cual los componentes de vista, salvo la pantalla de carga tienen un binding
	var app_initialized: Boolean = false;

    // CONTROLADORA
    var loadingControl: LoadingControl = null;
    var detailControl: DetailControl = null;

    //MODELO
    public var servicios: Servicio[] = bind this.req.servicios on replace {
        if (servicios != null) {
            (this.view as SearchView).setServicios(servicios);
			app_initialized = true;
            this.showView();
			visibilidad = true; //Volvemos las vistas visibles
        }
    };
    public var coleccion: ColeccionLibros = bind this.req.coleccion on replace {
        if (coleccion != null) {
            (this.view as SearchView).setColeccion(coleccion);
			(this.view as SearchView).hideLoading();
            (this.view as SearchView).showResult();
        }
    };
	//EXCEPTION MODELO
	public var exception: Exception = bind this.req.exception on replace {
		if (exception != null) {
			if (app_initialized) {
				this.showException(exception);
			}
			else {
				loadingControl.showException(exception);
			}
        }
	}

    override public function initControl (c1: Control, c2: Control) : Void {
        if (c1 instanceof LoadingControl and c2 instanceof DetailControl) {
            loadingControl = c1 as LoadingControl;
            detailControl = c2 as DetailControl;
        }
    }

    public function getServices () : Void {
        this.resetRequest();
        this.req.services();
    }
    public function search (service: String, query: String, pag_actual: Integer){
		this.search(service, query, pag_actual, "all");
	}
	public function search (service: String, query: String, pag_actual: Integer, criterio : String ) : Void {
		(this.view as SearchView).showLoading();
        this.resetRequest();
        this.req.search(service, query, pag_actual,criterio);
    }
    public function showDetail (service: String, id_libro: String) {
		(this.view as SearchView).showLoading();
        detailControl.detail(service, id_libro, "d/m/Y");
    }

    override public function showView () : Void {
        loadingControl.hideView();
        detailControl.hideView();
        super.showView();
		(this.view as SearchView).hideLoading();
    }

    override public function hideView () : Void {
        super.hideView();
		(this.view as SearchView).hideLoading();
    }
}
